package ca.ets.osgi.reservationservice;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 
 * @author HP_Administrator
 *
 */
public class RoomInfoReader {

	private ListRoomsInfo lri;
	
	public RoomInfoReader(){}
	
	/**
	 * Make the list of rooms info
	 */
	public void openList(){
		lri = ListRoomsInfo.getInstance();
		System.out.println("-------------Trying to load and fill the list of rooms informations-------------");
		try {
			File fXmlFile = new File("C://temp/ca.ets.osgi.reservationservice/src/ca/ets/osgi/reservationservice/roomInfo2.xml");
			//File fXmlFile = new File("C://eclipse/Workspace/ca.ets.osgi.reservationservice/src/ca/ets/osgi/reservationservice/roomInfo2.xml");
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fXmlFile);
			doc.getDocumentElement().normalize();
			NodeList roomList = doc.getElementsByTagName("Room");
			for (int temp = 0; temp < roomList.getLength(); temp++) {
				Node roomNode = roomList.item(temp);
				if (roomNode.getNodeType() == Node.ELEMENT_NODE) {
					Element elem = (Element) roomNode;
					RoomInfo room = new RoomInfo();
					String boo = getValue("availability",elem);
					boolean projector = false;
					
					if(boo.equalsIgnoreCase("true")){
						projector = true;
					}
					room.setRoomTaken(projector);
					room.setHourStart(getValue("hourStart",elem));
					room.setHourEnd(getValue("hourEnd",elem));
					room.setDate(getValue("date",elem));
					lri.addRoomInfoToList(room);
					

				}
				
			}
			System.out.println("-------------The list have been loaded-------------");
			System.out.println("");
			
		}catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
	    }
		
	}private static String getValue(String tag, Element element) {
		NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
		Node node = (Node) nodes.item(0);
		return node.getNodeValue();
	}
}
